package com.states
{
	import com.CharacterData;
	import com.FlaImage;
	import com.FlaText;
	import com.managers.FontManager;
	import com.PlayerData;
	import com.PrintCharSheet;
	import com.SkillList;
	import com.managers.XmlManager;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.Rectangle;
	import flash.net.FileFilter;
	import flash.net.FileReference;
	import flash.printing.PrintJob;
	import flash.printing.PrintJobOptions;
	import flash.text.TextFormat;

	public class MainTool extends State
	{
		private var _playerData:PlayerData;
		private var _characterData:Vector.<CharacterData>;
		private var _save:FlaText;
		private var _load:FlaText;
		private var _print:FlaText;
		private var _file:FileReference = new FileReference();
		private var _tab0:FlaText;
		private var _tab1:FlaText;
		private var _tab2:FlaText;
		private var _actionBox:FlaImage;
		
		private var _printChar:PrintCharSheet = new PrintCharSheet();
		
		public function MainTool()
		{
			_playerData = new PlayerData();
			addChild(_playerData);
			_characterData = new Vector.<CharacterData>();
			for (var i:int = 0; i < 3; i++) {
				_characterData.push(new CharacterData());
				addChild(_characterData[i]);
				_characterData[i].x = 20;
				_characterData[i].y = 350;
				_characterData[i]._name.addEventListener(Event.CHANGE, onNameChange);
			}
			
			_characterData[1].visible = false;
			_characterData[2].visible = false;
			
			_playerData.x = 200;
			_playerData.y = 40;
			
			_actionBox = new FlaImage();
			_actionBox.graphics.beginFill(0xffffff);
			_actionBox.graphics.lineStyle(3, 0x000000);
			_actionBox.graphics.drawRoundRect(0, 0, 150, 200, 10, 10);
			_actionBox.graphics.endFill();
			addChild(_actionBox);
			_actionBox.x = 20;
			_actionBox.y = 20;
			
			_save = new FlaText("Save", new TextFormat(FontManager.Default, 18, null, true, null, null, null, null, "center"));
			_save.x = 45;
			_save.y = 45;
			_save.border = true;
			_save.background = true;
			_save.backgroundColor = 0xdddddd;
			addChild(_save);
			
			_save.addEventListener(MouseEvent.CLICK, onSave);
			
			_load = new FlaText("Load", new TextFormat(FontManager.Default, 18, null, true, null, null, null, null, "center"));
			_load.x = 45;
			_load.y = 105;
			_load.border = true;
			_load.background = true;
			_load.backgroundColor = 0xdddddd;
			addChild(_load);
			
			_load.addEventListener(MouseEvent.CLICK, onLoad);
			
			_print = new FlaText("Print", new TextFormat(FontManager.Default, 18, null, true, null, null, null, null, "center"));
			_print.x = 45;
			_print.y = 165;
			_print.border = true;
			_print.background = true;
			_print.backgroundColor = 0xdddddd;
			addChild(_print);
			
			_print.addEventListener(MouseEvent.CLICK, onPrint);
			
			if (_characterData[0]._name.text != "") {
				_tab0 = new FlaText(_characterData[0]._name.text, new TextFormat(FontManager.Default, 18, null, true, null, null, null, null, "center"));
			}else {
				_tab0 = new FlaText("Character 1", new TextFormat(FontManager.Default, 18, null, true, null, null, null, null, "center"));
			}
			if (_characterData[1]._name.text != "") {
				_tab1 = new FlaText(_characterData[1]._name.text, new TextFormat(FontManager.Default, 18, null, true, null, null, null, null, "center"));
			}else {
				_tab1 = new FlaText("Character 2", new TextFormat(FontManager.Default, 18, null, true, null, null, null, null, "center"));
			}
			if (_characterData[2]._name.text != "") {
				_tab2 = new FlaText(_characterData[2]._name.text, new TextFormat(FontManager.Default, 18, null, true, null, null, null, null, "center"));
			}else {
				_tab2 = new FlaText("Character 3", new TextFormat(FontManager.Default, 18, null, true, null, null, null, null, "center"));
			}
			addChild(_tab0);
			addChild(_tab1);
			addChild(_tab2);
			
			_tab0.x = 50;
			_tab0.y = 250;
			_tab1.x = 350;
			_tab1.y = 250;
			_tab2.x = 650;
			_tab2.y = 250;
			
			_tab0.border = true;
			_tab0.background = true;
			_tab0.backgroundColor = 0xaaaaaa;
			_tab0.width = 250;
			_tab1.border = true;
			_tab1.background = true;
			_tab1.backgroundColor = 0xdddddd;
			_tab1.width = 250;
			_tab2.border = true;
			_tab2.background = true;
			_tab2.backgroundColor = 0xdddddd;
			_tab2.width = 250;
			
			_tab0.addEventListener(MouseEvent.CLICK, tab1);
			_tab1.addEventListener(MouseEvent.CLICK, tab2);
			_tab2.addEventListener(MouseEvent.CLICK, tab3);
		}
		
		private function onPrint(e:Event):void {
			_printChar._name.text = "Name: " + _playerData._playerName.text;
			_printChar._waiver.text = "Signed Waiver?: " + _playerData._waiver.text;
			_printChar._referral.text = "Brought by: " + _playerData._referral.text;
			_printChar._frags.text = "Frags: " + _playerData._frags.text;
			_printChar._blankets.text = "Blankets: " + _playerData._blankets.text;
			
			for (var i:int = 0; i < 3; i++) {
				if (_characterData[i].visible){
					_printChar._charName.text = "Character Name: " + _characterData[i]._name.text;
					_printChar._trueName.text = "True Name: " + _characterData[i]._trueName.text;
					_printChar._race.text = "Race: " + _characterData[i]._race.text;
					_printChar._class.text = "Class: " + _characterData[i]._class.text;
					_printChar._level.text = "Level: " + _characterData[i]._level.text;
					_printChar._hp.text = "Hit Points: " + _characterData[i]._hp.text;
					
					_printChar._deaths.text = "Deaths: " + _characterData[i]._deaths.text;
					_printChar._pp.text = "Banked Production Points: " + _characterData[i]._pp.text;
					_printChar._charBlankets.text = "Total Blankets Applied: " + _characterData[i]._blankets.text;
					_printChar._totalCP.text = "Total CP: " + _characterData[i]._totalCP.text;
					_printChar._spentCP.text = "Spent CP: " + _characterData[i]._spentCP.text;
					_printChar._freeCP.text = "Free CP: " + _characterData[i]._freeCP.text;
					
					_printChar._skills.text = "Skills:\n";
					for (var j:int = 0; j < _characterData[i]._characterSkills._skills.length; j++) {
						_printChar._skills.text = _printChar._skills.text + _characterData[i]._characterSkills._skills[j].name + " " + _characterData[i]._characterSkills._skills[j].cost + "\n";
					}
				}
			}
			
			addChild(_printChar);
			/*var printJob:PrintJob = new PrintJob();
			var numPages:int = 0;
			var printArea:Rectangle;
			var printHeight:Number;
			var printY:int = 0;
			
			if ( printJob.start() ) {
				if (_printChar.width > printJob.pageWidth) {
						_printChar.width = printJob.pageWidth;
						_printChar.scaleY = _printChar.scaleX;
				}
				
				printArea = new Rectangle(0, 0, printJob.pageWidth/_printChar.scaleX, printJob.pageHeight/_printChar.scaleY);
				numPages = Math.ceil(_printChar.height / printJob.pageHeight);
				
				for (var i:int = 0; i < numPages; i++) {
						printJob.addPage(_printChar, printArea);
						printArea.y += printArea.height;
				}
				
				printJob.send();
				printJob = null;
			}*/
		}
		
		private function onNameChange(e:Event = null):void {
			for (var i:int = 0; i < 3; i++) {
				if (_characterData[i]._name.text != "") {
					this["_tab" + i].text = _characterData[i]._name.text;
				}else {
					this["_tab" + i].text = "Character " + (i+1);
				}
			}
		}
		
		private function tab1(e:MouseEvent):void {
			_tab0.backgroundColor = 0xaaaaaa;
			_tab1.backgroundColor = 0xdddddd;
			_tab2.backgroundColor = 0xdddddd;
			_characterData[0].visible = true;
			_characterData[1].visible = false;
			_characterData[2].visible = false;
		}
		private function tab2(e:MouseEvent):void {
			_tab0.backgroundColor = 0xdddddd;
			_tab1.backgroundColor = 0xaaaaaa;
			_tab2.backgroundColor = 0xdddddd;
			_characterData[0].visible = false;
			_characterData[1].visible = true;
			_characterData[2].visible = false;
		}
		private function tab3(e:MouseEvent):void {
			_tab0.backgroundColor = 0xdddddd;
			_tab1.backgroundColor = 0xdddddd;
			_tab2.backgroundColor = 0xaaaaaa;
			_characterData[0].visible = false;
			_characterData[1].visible = false;
			_characterData[2].visible = true;
		}
		
		private function onLoad(e:MouseEvent):void {
			_file.browse([new FileFilter("xml", "*.xml", "*.xml")]);
			
			_file.addEventListener(Event.SELECT, onSelected);
		}
		
		private function onSelected(e:Event):void {
			_file.removeEventListener(Event.SELECT, onSelected);
			trace(_file.name);
			XmlManager.instance.loadFile("PlayerData/" + _file.name, "data");
			XmlManager.instance.addEventListener("data_LOADED", onLoaded);
		}
		
		private function onLoaded(e:Event):void {
			XmlManager.instance.removeEventListener("data_LOADED", onLoaded);
			
			removeChild(_playerData);
			
			_playerData = new PlayerData();
			addChild(_playerData);
			for (var i:int = 0; i < 3; i++) {
				removeChild(_characterData[i]);
				_characterData[i] = new CharacterData();
				addChild(_characterData[i]);
				_characterData[i].x = 20;
				_characterData[i].y = 350;
				_characterData[i]._name.addEventListener(Event.CHANGE, onNameChange);
			}
			
			_playerData.x = 200;
			_playerData.y = 40;
			
			_playerData._playerName.text = XmlManager.instance.getXML("data").playerName;
			_playerData._waiver.text = XmlManager.instance.getXML("data").waiver;
			_playerData._referral.text = XmlManager.instance.getXML("data").referral;
			_playerData._frags.text = XmlManager.instance.getXML("data").frags;
			_playerData._blankets.text = XmlManager.instance.getXML("data").blankets;
			
			for (i = 0; i < 3; i++) {
				_characterData[i]._name.text = XmlManager.instance.getXML("data").character[i].name;
				_characterData[i]._trueName.text = XmlManager.instance.getXML("data").character[i].trueName;
				_characterData[i]._race.text = XmlManager.instance.getXML("data").character[i].race;
				_characterData[i]._class.text = XmlManager.instance.getXML("data").character[i].charClass;
				_characterData[i]._deaths.text = XmlManager.instance.getXML("data").character[i].deaths;
				_characterData[i]._pp.text = XmlManager.instance.getXML("data").character[i].pp;
				_characterData[i]._blankets.text = XmlManager.instance.getXML("data").character[i].blankets;
				
				for (var j:int = 0; j < XmlManager.instance.getXML("data").character[i].skills.skill.length(); j++) {
					_characterData[i]._characterSkills.loadSkill(
						XmlManager.instance.getXML("data").character[i].skills.skill[j].cost, 
						XmlManager.instance.getXML("data").character[i].skills.skill[j].name, 
						XmlManager.instance.getXML("data").character[i].skills.skill[j].level, 
						XmlManager.instance.getXML("data").character[i].charClass);
				}
				_characterData[i].update();
			}
			onNameChange();
			tab1(null);
		}
		
		private function onSave(e:MouseEvent):void {
			var xml:XML = <data/>;
			var data:XML = <data/>;
			
			var playerName:XML = <playerName/>;
			playerName.appendChild(_playerData._playerName.text);
			var waiver:XML = <waiver/>;
			waiver.appendChild(_playerData._waiver.text);
			var referral:XML = <referral/>;
			referral.appendChild(_playerData._referral.text);
			var frags:XML = <frags/>;
			frags.appendChild(_playerData._frags.text);
			var playerBlankets:XML = <blankets/>;
			playerBlankets.appendChild(_playerData._blankets.text);
			
			data.appendChild(playerName);
			data.appendChild(waiver);
			data.appendChild(referral);
			data.appendChild(frags);
			data.appendChild(playerBlankets);
			
			for (var i:int = 0; i < 3; i++) {
				var character:XML = <character/>;
				
				var name:XML = <name/>;
				name.appendChild(_characterData[i]._name.text);
				var trueName:XML = <trueName/>;
				trueName.appendChild(_characterData[i]._trueName.text);
				var race:XML = <race/>;
				race.appendChild(_characterData[i]._race.text);
				var charClass:XML = <charClass/>;
				charClass.appendChild(_characterData[i]._class.text);
				var deaths:XML = <deaths/>;
				deaths.appendChild(_characterData[i]._deaths.text);
				var pp:XML = <pp/>;
				pp.appendChild(_characterData[i]._pp.text);
				var blankets:XML = <blankets/>;
				blankets.appendChild(_characterData[i]._blankets.text);
				
				character.appendChild(name);
				character.appendChild(trueName);
				character.appendChild(race);
				character.appendChild(charClass);
				character.appendChild(deaths);
				character.appendChild(pp);
				character.appendChild(blankets);
				character.appendChild(_characterData[i]._characterSkills.skills);
				
				data.appendChild(character);
			}
			
			xml.appendChild(data);
				
			_file.save(xml, _playerData._playerName.text + ".xml");
		}
	}
}